﻿using UnityEngine;
using System.Collections;
using cn.sharesdk.unity3d;
using UnityEngine.UI;

public class GameShare : MonoBehaviour {

    private ShareSDK ssdk;
    public Text message;

    private GameObject qqShare;
    private GameObject weiShare;
    private GameObject friendsShare;

    void Start()
    {
        ssdk = gameObject.GetComponent<ShareSDK>();
        //授权回调事件
        ssdk.authHandler = OnAuthResultHandler;
        //分享回调事件
        ssdk.shareHandler = OnShareResultHandler;
        //用户信息事件
        ssdk.showUserHandler = OnGetUserInfoResultHandler;
        ssdk.getFriendsHandler = OnGetFriendsResultHandler;
        ssdk.followFriendHandler = OnFollowFriendResultHandler;

        qqShare = gameObject.transform.Find("QQShare").gameObject;
        weiShare = gameObject.transform.Find("WeiShare").gameObject;
        friendsShare = gameObject.transform.Find("WeiFriendShare").gameObject;

        qqShare.GetComponent<UIButton>().onClick.Add(new EventDelegate(OnClickShareQQ));
        weiShare.GetComponent<UIButton>().onClick.Add(new EventDelegate(OnClickShareWeiChat));
        friendsShare.GetComponent<UIButton>().onClick.Add(new EventDelegate(OnClickShareFriends));
    }

    //分享
    public void OnClickShare()
    {
        ShareContent content = new ShareContent();

        //这个地方要参考不同平台需要的参数    可以看ShareSDK提供的   分享内容参数表.docx
        content.SetText("快来和我一起玩这个游戏吧！");                            //分享文字
        content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/4.jpg");   //分享图片
        content.SetTitle("标题title");                                            //分享标题
        content.SetTitleUrl("http://www.mob.com");
        content.SetSite("Mob-ShareSDK");
        content.SetSiteUrl("http://www.mob.com");
        content.SetUrl("http://www.sina.com");                                    //分享网址
        content.SetComment("描述");
        content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");//分享类型为音乐时用
        content.SetShareType(ContentType.Webpage);


        //shareSdk.ShowPlatformList(null, content, 100, 100);                      //弹出分享菜单选择列表
        //ssdk .ShowShareContentEditor(PlatformType.QQ, content);                 //指定平台直接分享
        //不同平台分享不同内容
        ShareContent customizeShareParams = new ShareContent();
        customizeShareParams.SetText("Sina share content");
        customizeShareParams.SetImageUrl("http://git.oschina.net/alexyu.yxj/MyTmpFiles/raw/master/kmk_pic_fld/small/107.JPG");
        customizeShareParams.SetShareType(ContentType.Image);
        customizeShareParams.SetObjectID("SinaID");
        content.SetShareContentCustomize(PlatformType.SinaWeibo, customizeShareParams);

        ssdk.ShowPlatformList(null, content, 100, 100);
    }

    ShareContent getShareContent()
    {
        ShareContent content = new ShareContent();

        //标题
        content.SetTitle("逗游动漫");
        content.SetTitleUrl("http://www.mob.com");

        // content
        content.SetText("this is a test string.");

        // image
        content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/1.jpg");

        // url
        content.SetUrl("http://www.mob.com");

        content.SetSite("Mob-ShareSDK");
        content.SetSiteUrl("http://www.mob.com");


        content.SetShareType(ContentType.Webpage);

        return content;
    }

    public void OnClickShareQQ()
    {
        Debug.Log(">>>>>>  OnClickShareQQ ");  
        ssdk.ShareContent(PlatformType.QQ, getShareContent());
    }

    public void OnClickShareWeiChat()
    {
        Debug.Log(">>>>>>  OnClickShareWeiChat ");
        ssdk.ShareContent(PlatformType.WeChat, getShareContent());
    }

    public void OnClickShareFriends()
    {
        Debug.Log(">>>>>>  OnClickShareFriends ");
        ssdk.ShareContent(PlatformType.WeChatMoments, getShareContent());
    }

    //授权
    public void OnAuthClick()
    {
        //请求QQ授权//请求这个授权是为了获取用户信息来第三方登录
        ssdk.Authorize(PlatformType.QQ);
    }

    public void OnClickLoginWeiChat()
    {
#if UNITY_STANDALONE

#else

        if (!ssdk.IsAuthorized(PlatformType.WechatPlatform))
        {
            ssdk.Authorize(PlatformType.WechatPlatform);
        }
        else
        {
            postAuthData();
        }
#endif
    }

    void postAuthData()
    {
#if UNITY_STANDALONE

#else
        //UIHelper.showLoadingDialog();
        //		Hashtable authData = ssdk.GetAuthInfo (PlatformType.WechatPlatform);
        ssdk.GetUserInfo(PlatformType.WechatPlatform);

#endif

    }

    //授权结果回调
    void OnAuthResultHandler(int reqID,ResponseState state,PlatformType type,Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print("authorize success !" + "Platform :" + type);
            message.text = ("authorize success !" + "Platform :" + type);
            //授权成功的话，获取用户信息
            ssdk.GetUserInfo(type);
        }
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            message.text = ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            message.text = ("cancel !");
        }
    }

    //获取用户信息
    void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            //获取成功的话 可以写一个类放不同平台的结构体，用PlatformType来判断，用户的Json转化成结构体，来做第三方登录。
            print("get user info result :");
            //print(MiniJSON.jsonEncode(result));
            print("Get userInfo success !Platform :" + type);
            switch (type )
            {
                case PlatformType.QQ:
                    message.text = (MiniJSON.jsonEncode(result));
                    break;
                case PlatformType.WeChat:
                    message.text = (MiniJSON.jsonEncode(result));
                    break;
            }

        }
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            message.text = ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			 message.text = ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            message.text = ("cancel !");
        }
    }

    //分享结果回调
    void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        //成功
        if (state == ResponseState.Success)
        {
            message.text = ("share successfully - share result :");
            message.text = (MiniJSON.jsonEncode(result));        
        }
        //失败
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            message.text = ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			message.text =  ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        //关闭
        else if (state == ResponseState.Cancel)
        {
            message.text = ("cancel !");
        }
    }


    void OnGetFriendsResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print("get friend list result :");
            print(MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            print("cancel !");
        }
    }

    void OnFollowFriendResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print("Follow friend successfully !");
        }
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            print("cancel !");
        }
    }
}

/*

//QQ用户信息结构体
 struct QQUser 
{
     public string yellow_vip_level;
     public string msg;
     public string province;
     public string gender;
     public string is_yellow_year_vip;
     public int is_lost;
     public string nickname;
     public int ret;
     public string level;
     public string city;
     public string figureurl;
     public string figureurl_1;
     public string figureurl_2;
     public string figureurl_qq_1;
     public string figureurl_qq_2;
     public string vip;
     public string is_yellow_vip;
}
*/
